package hk.m4s.lr.repair.test.ui.user;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import framework.common.Constant;
import framework.common.baseui.UeBaseActivity;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.WorkBean;
import hk.m4s.lr.repair.test.service.AccountSerive;
import hk.m4s.lr.repair.test.ui.adapter.WorkAdapter;
import hk.m4s.lr.repair.test.utils.ToastUtil;
import okhttp3.Response;

public class SetPayQuestionActivity extends UeBaseActivity implements OnClickListener {
    private EditText pwdText1, pwdText2, pwText3;
    private RelativeLayout click_q1, click_q2, click_q3, select_question;
    private TextView nextText, title, user_label, user_label2, user_labels,
            home_next_order;
    private Button cancle, confirm;
    private String pwd1, pwd2;
    private Context context;
    private ListView listView;
    private WorkAdapter workAdapter;
    private List<WorkBean> workList = new ArrayList<>();
    private String tag = "";
    private String question_one_title = "", question_key_one = "", answer_one,
            question_key_two = "", question_two_title = "", answer_two = "",
            question_key_three = "", question_three_title = "",
            answer_three = "";
    private String repeat_password = "", password = "";
    private ProgressDialog progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        context = this;
        baseSetContentView(R.layout.activity_set_pay_question);
        showGoBackBtn();
        hiddenFooter();
        setTitleText("密码保护设置");
        pwdText1 = (EditText) findViewById(R.id.ppe_pwd1);
        pwdText2 = (EditText) findViewById(R.id.ppe_pwd2);
        pwText3 = (EditText) findViewById(R.id.ppe_pwd3);
        click_q1 = (RelativeLayout) findViewById(R.id.home_order_wx);
        click_q2 = (RelativeLayout) findViewById(R.id.home_order_w2);
        click_q3 = (RelativeLayout) findViewById(R.id.home_order_w3);
        select_question = (RelativeLayout) findViewById(R.id.select_brank);
        cancle = (Button) findViewById(R.id.btn_canle1);
        confirm = (Button) findViewById(R.id.btn_confirm1);
        listView = (ListView) findViewById(R.id.listview);
        title = (TextView) findViewById(R.id.title);
        user_label = (TextView) findViewById(R.id.user_label);
        home_next_order = (TextView) findViewById(R.id.home_next_order);
        user_label2 = (TextView) findViewById(R.id.user_label2);
        user_labels = (TextView) findViewById(R.id.user_labels);
        nextText = (TextView) findViewById(R.id.home_next_order);
        nextText.setOnClickListener(this);
        listView.setOnItemClickListener(clickItems);
        repeat_password = getIntent().getStringExtra("pwd2");
        password = getIntent().getStringExtra("pwd1");
        workAdapter = new WorkAdapter(context, workList);
        listView.setAdapter(workAdapter);
        //findAllProblem();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.home_order_wx:
                tag = "1";
                select_question.setVisibility(View.VISIBLE);
                break;
            case R.id.home_order_w2:
                tag = "2";
                select_question.setVisibility(View.VISIBLE);
                break;
            case R.id.home_order_w3:
                tag = "3";
                select_question.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_canle1:
                select_question.setVisibility(View.GONE);
                if (tag.equals("1")) {
                    user_label.setText("");
                } else if (tag.equals("2")) {
                    user_label2.setText("");
                } else {
                    user_labels.setText("");
                }
                break;
            case R.id.btn_confirm1:
                select_question.setVisibility(View.GONE);

                break;
            case R.id.home_next_order:
                if (pwdText1.getText().toString().trim().equals("")) {
                    Toast.makeText(context, "请回答问题!", Toast.LENGTH_SHORT).show();
                    return;
                } else if (pwdText2.getText().toString().trim().equals("")) {
                    Toast.makeText(context, "请回答问题!", Toast.LENGTH_SHORT).show();
                    return;
                } else if (pwText3.getText().toString().trim().equals("")) {
                    Toast.makeText(context, "请回答问题!", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    answer_one = pwdText1.getText().toString().trim();
                    answer_two = pwdText2.getText().toString().trim();
                    answer_three = pwText3.getText().toString().trim();
                    //getSetPayPassWord();
                }

                break;
            default:
                break;
        }
    }

//    public void getSetPayPassWord() {
//        Map<String, Object> parmas = new HashMap<>();
//        parmas.put("device_type", Constant.device_type);
//        parmas.put("userId", SharedPreferencesUtils.getSharedPreferences(Constant.userId, ""));
//        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
//        parmas.put("problemOne", question_key_one);
//        parmas.put("answerOne", answer_one);
//        parmas.put("problemTwo", question_key_two);
//        parmas.put("answerTwo", answer_two);
//        parmas.put("problemThree", question_key_three);
//        parmas.put("answerThree", answer_three);
//        parmas.put("payPassWord", password);
//        AccountSerive.setPayPassword(context, parmas, new ResponseCallback<GoodsModel>() {
//            @Override
//            public void onSuccess(GoodsModel userinfoModel) {
//                ToastUtil.toast(context, "密码设置成功");
//                SharedPreferencesUtils.addgetSharedPreferences("pwd", repeat_password);
//                finish();
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
//
//            }
//        });
//
//    }

//    public void findAllProblem() {
//        Map<String, Object> parmas = new HashMap<>();
//        parmas.put("device_type", Constant.device_type);
//        parmas.put("userId", SharedPreferencesUtils.getSharedPreferences(Constant.userId, ""));
//        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
//        AccountSerive.findAllProblem(context, parmas, new ResponseCallback<WorkBean>() {
//            @Override
//            public void onSuccess(WorkBean workBean) {
//                if (workBean != null) {
//                    if (workBean.getList() != null) {
//                        workList.addAll(workBean.getList());
//                    }
//                }
//                workAdapter.notifyDataSetChanged();
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
//
//            }
//        });
//
//    }
    OnItemClickListener clickItems = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {
            // TODO Auto-generated method stub
            if (workAdapter != null) {
                workAdapter.flags = arg2;
                workAdapter.notifyDataSetChanged();
                WorkBean bean = workAdapter.list.get(arg2);
                if (tag.equals("1")) {
                    user_label.setText(bean.getName());
                    question_key_one = bean.getId();
                    question_one_title = bean.getName();
                } else if (tag.equals("2")) {
                    user_label2.setText(bean.getName());
                    question_key_two = bean.getId();
                    question_two_title = bean.getName();
                } else {
                    user_labels.setText(bean.getName());
                    question_key_three = bean.getId();
                    question_three_title = bean.getName();
                }

            }

        }
    };
}
